//
//  PIDrawerView.m
//  PIImageDoodler
//
//  Created by Pavan Itagi on 07/03/14.
//  Copyright (c) 2014 Pavan Itagi. All rights reserved.
//

#import "PIDrawerView.h"

@interface PIDrawerView ()
{
    CGPoint previousPoint;
    CGPoint currentPoint;
}
//@property (nonatomic, strong) UIImage * viewImage;
@end

@implementation PIDrawerView

- (void)awakeFromNib
{
    [self initialize];
}

- (void)drawRect:(CGRect)rect
{
    [self.viewImage drawInRect:self.bounds];
}

#pragma mark - setter methods
- (void)setDrawingMode:(DrawingMode)drawingMode
{
    _drawingMode = drawingMode;
}

#pragma mark - Private methods
- (void)initialize
{
    currentPoint = CGPointMake(0, 0);
    previousPoint = currentPoint;
    
    _drawingMode = DrawingModeNone;
    
    _selectedColor = [UIColor blackColor];
}

- (void)eraseLine
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.viewImage drawInRect:self.bounds];
    
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    previousPoint = currentPoint;
    
    [self setNeedsDisplay];
}

- (void)drawTextField
{
    NSUInteger w = currentPoint.x - previousPoint.x;
    
    NSUInteger h = currentPoint.y - previousPoint.y;
    
    self.txt = [[UITextField alloc] initWithFrame:CGRectMake(previousPoint.x, previousPoint.y, w, h)];
    
    self.txt.font = [UIFont systemFontOfSize:30];
    
    self.txt.tintColor = [UIColor redColor];
    
    [self addSubview:self.txt];
    
    [self setNeedsDisplay];
}

//动态添加文本框
- (void)drawTextView
{
    if (self.txtView) {
        [self.txtView removeFromSuperview];
    }
    
    CGPoint leftTop = CGPointMake(0, 0);
    CGPoint rightDown = CGPointMake(0, 0);
    
    if (currentPoint.x > previousPoint.x && currentPoint.y > previousPoint.y)
    {
        leftTop = CGPointMake(previousPoint.x, previousPoint.y);
        rightDown = CGPointMake(currentPoint.x, currentPoint.y);
    }
    else if(currentPoint.x > previousPoint.x && currentPoint.y < previousPoint.y)
    {
        leftTop = CGPointMake(previousPoint.x, currentPoint.y);
        rightDown = CGPointMake(currentPoint.x, previousPoint.y);
    }
    else if(currentPoint.x < previousPoint.x && currentPoint.y > previousPoint.y)
    {
        leftTop = CGPointMake(currentPoint.x, previousPoint.y);
        rightDown = CGPointMake(previousPoint.x, currentPoint.y);
    }
    else
    {
        leftTop = CGPointMake(currentPoint.x, currentPoint.y);
        rightDown = CGPointMake(previousPoint.x, previousPoint.y);
    }
    
    NSUInteger w = rightDown.x - leftTop.x;
    
    NSUInteger h = rightDown.y - leftTop.y;
    
    self.txtView = [[UITextView alloc] initWithFrame:CGRectMake(leftTop.x, leftTop.y, w, h)];
    
    self.txtView.font = [UIFont systemFontOfSize:25];
    
    self.txtView.tintColor = self.color;
    
    self.txtView.textAlignment = NSTextAlignmentLeft;
    
    self.txtView.backgroundColor = [UIColor clearColor];
    
    self.txtView.layer.borderColor = self.color.CGColor;
    self.txtView.layer.borderWidth = 1.0;
    self.txtView.layer.cornerRadius = 5.0;
    
    [self addSubview:self.txtView];
    
    [self setNeedsDisplay];
    
    [self.txtView becomeFirstResponder];
}

- (void)drawLineNew
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.viewImage drawInRect:self.bounds];
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    previousPoint = currentPoint;
    
    [self setNeedsDisplay];
}

- (void)handleTouches
{
    if (self.drawingMode == DrawingModeNone) {
        // do nothing
    }
    else if (self.drawingMode == DrawingModePaint) {
        [self drawLineNew];
    }
    else if (self.drawingMode == DrawingModeTxt) {
        [self drawTextView];
    }
    else
    {
        [self eraseLine];
    }
}

#pragma mark - Touches methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint p = [[touches anyObject] locationInView:self];
    previousPoint = p;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    currentPoint = [[touches anyObject] locationInView:self];
    
    if (self.drawingMode != DrawingModeTxt) {
        [self handleTouches];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    currentPoint = [[touches anyObject] locationInView:self];
    
    [self handleTouches];
}

@end
